package com.whcstar.springcloud.eurekaconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/operate")
public class OperateController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/add")
    public Integer add(Integer a, Integer b) {
        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-service-provide");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/operate/add?a={a}&b={b}";
        System.out.println(url);
        Map<String, Integer> map = new HashMap<>();
        map.put("a", a);
        map.put("b", b);
        Integer result = restTemplate.getForObject(url, Integer.class, map);
        return result;
    }

}
